#include "view_music.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "app_timer.h"
#include "view_music.inc"
#include "times/view_time_0.h"

void view_music_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 207, FILE_IMG_back_0_207);
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_music_contorl, 50);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 24, 88, FILE_IMG_Music_24_88);
    }
    else
    {
        disp_show_localize(DISP_LFS_FILE_INDEX_3, localize_string_key_music_contorl_not_connect, 51);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, 69, 35, FILE_IMG_iocn_69_35);
    }
}
view_event_result_t view_music_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
        if (ble_if_connected())
        {
            music_ctrl_action(music_ctrl_action_next);
        }
        return view_event_result_handled;
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
        if (ble_if_connected())
        {
            music_ctrl_action(music_ctrl_action_previous);
        }
        return view_event_result_handled;
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP:
    case BUTTON_EVENT_TP_TAP_LONG:
        if (ble_if_connected())
        {
            music_ctrl_action(music_ctrl_action_start_stop);
        }
        return view_event_result_handled;
#endif
    case event_view_exit:
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
